<!--
 * @author  tanyuze
 评论列表
-->
<template>
  <div class="class-comment">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model.trim="listQuery.content" placeholder="输入评论内容搜索" class="search-input"
          @keyup.enter.native="handleFilter" />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>
    </el-collapse-transition>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>交流评论</span>
      </section>
      <section class="main-buttons-btns" />
      <el-button style="margin-left: 10px;" @click="handleCancel">返回</el-button>
    </section>

    <div class="classify-tab">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane v-for="(item, index) in dict.PLZT" :key="index" :label="item.dictName" :name="item.dictValue" />
      </el-tabs>
    </div>

    <section class="main-table">
      <el-table :data="list" :list-loading="listLoading" style="width: 100%">
        <el-table-column type="index" width="50" label="序号" />
        <el-table-column prop="content" label="评论内容">
          <template slot-scope="scope">
            <el-row>
              <el-col :span="24">评论内容：{{ scope.row.content }}</el-col>
              <el-col :span="24">评论人：{{ scope.row.userName }}</el-col>
              <el-col :span="12">评论状态：{{ scope.row.statusName }}</el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="userImgs" label="评论图片">
          <template slot-scope="scope">
            <el-image v-for="item in scope.row.userImgs ? scope.row.userImgs.split(',') : []" :key="item"
              class="comment-img" :z-index="99999" :src="view + item" :preview-src-list="[view + item]">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="crtTime" label="评论时间" />
        <el-table-column label="操作" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleShow(scope.row)">溯源</el-button>
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">修改状态</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
        @pagination="getList" />
    </section>

    <!--状态设置-->
    <el-dialog v-el-drag-dialog :close-on-click-modal="false" :visible.sync="dialog4comment" title="评论设置" center>
      <el-form ref="form" label-width="150px">
        <el-form-item label="评论状态">
          <el-radio-group v-model="commentStatus">
            <el-radio v-for="(item, index) in dict.PLZT" :key="index" :label="item.dictValue">{{ item.dictName }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="commentStatus === 'PLZT03'" label="审核原因" prop="examineReason">
          <el-input v-model="examineReason" type="textarea" :rows="4" placeholder="请输入不通过原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="dialog4comment = false">关 闭</el-button>
        <el-button type="primary" style="padding: 8px 30px;" @click="handleCommit()">确 认</el-button>
      </div>
    </el-dialog>
    <!--溯源-->
    <el-dialog v-el-drag-dialog :close-on-click-modal="false" :visible.sync="dialog4commentParent" title="评论设置" center
      width="80%">
      <div class="main-table">
        <el-table :data="list4parent" :list-loading="listLoading4parent" style="width: 100%">
          <el-table-column type="index" width="50" />
          <el-table-column prop="content" label="评论内容">
            <template slot-scope="scope">
              <el-col :span="24">评论内容：{{ scope.row.content }}</el-col>
              <el-col :span="24">评论人：{{ scope.row.userRealName }}</el-col>
              <el-col :span="12">评论状态：{{ scope.row.statusName }}</el-col>
            </template>
          </el-table-column>
          <el-table-column prop="userImgs" label="评论图片">
            <template slot-scope="scope">
              <el-image v-for="item in scope.row.userImgs ? scope.row.userImgs.split(',') : []" :key="item"
                class="comment-img" :z-index="99999" :src="view + item" :preview-src-list="[view + item]">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="crtTime" label="评论时间" />
          <el-table-column label="操作" fixed="right" width="80">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="handleUpdate(scope.row)">修改状态</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination v-show="total4parent > 0" :total="total4parent" :page.sync="listQuery4parent.page"
        :limit.sync="listQuery4parent.limit" @pagination="getList4parent" />
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="dialog4commentParent = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { queryPage, examine, getDictByCode, queryPage4 } from '../api'
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'ClassAudit',
  components: { Pagination },
  directives: { elDragDialog },
  data() {
    return {
      view: '/API-DOCUMENT/document/file/download?id=',
      searchshow: true,
      dialog4comment: false, // 修改状态
      dialog4commentParent: false, // 溯源

      // 列表
      commentsId: '',
      listQuery: {
        page: 1,
        limit: 10,
        content: ''
      },
      list: [],
      total: 0,
      listLoading: false,

      // 溯源
      listQuery4parent: {
        page: 1,
        limit: 10
      },
      listLoading4parent: false,
      list4parent: [],
      total4parent: 0,

      commentStatus: 'PLZT01',
      dict: {
        PLZT: []
      },
      activeTab: 'PLZT01',
      classId: '',
      examineReason: ''
    }
  },
  async created() {
    if (!this.$route.query.id) {
      this.$message({
        showClose: true,
        message: '参数异常！',
        type: 'warning',
        onClose: () => {
          this.$router.back()
        }
      })
      return
    } else {
      this.classId = this.$route.query.id
      for (const key in this.dict) {
        await this.getDict(key)
      }
      await this.getList()
    }
  },
  methods: {
    /**
     * 字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((response) => {
            this.dict[code] = response.data.data[0].list
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    handleTabClick() {
      this.getList()
    },
    /**
     * 返回
     */
    handleCancel() {
      this.$router.back()
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        status: this.activeTab,
        content: this.listQuery.content,
        classId: this.classId
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.list = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },

    filtercolumn(scope, item) {
      return scope.row[item]
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.content = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 修改状态
     */
    handleUpdate(row) {
      this.dialog4comment = true
      this.commentsId = row.id
    },
    /**
     * 溯源按钮
     */
    handleShow(row) {
      this.parentId = row.id
      this.dialog4commentParent = true
      this.getList4parent()
    },
    /**
     * 溯源
     */
    getList4parent(obj) {
      this.listLoading4parent = true
      var query = {
        page: obj ? obj.page : this.listQuery4parent.page,
        pageSize: obj ? obj.limit : this.listQuery4parent.limit,
        commentAncestor: this.parentId
      }
      return new Promise((resolve, reject) => {
        queryPage4(query)
          .then((response) => {
            this.list4parent = response.data.data.rows
            this.total4parent = response.data.data.total
            this.listLoading4parent = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading4parent = false
          })
      })
    },

    /**
     *确认审核评论
     */
    handleCommit() {
      if(this.commentStatus === 'PLZT03') {
        if (this.examineReason === "") {
          this.$message({
            message: '请填写审核原因！',
            type: 'error',
            duration: 2000
          })
          return
        }
      }
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        examine({
          id: this.commentsId,
          status: this.commentStatus,
          examineReason: this.examineReason
        })
          .then((res) => {
            loading.close()
            if (res.data.code === '200') {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.dialog4comment = false
              this.getList()
            }
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    }
  }
}
</script>

<style lang="scss">
.class-comment {
  padding: 10px;

  .main-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            &.el-date-editor input {
              padding-left: 30px;
            }

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin: 10px 0;

    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }

  .main-table {
    .comment-img {
      width: auto;
      height: auto;
      margin-right: 10px;

      >img {
        width: 100px;
        height: 100px;
      }

      .el-icon-circle-close {
        color: #fff;
      }
    }
  }
}
</style>
